package com.fau.wmbj.server.storage;


import com.fau.wmbj.server.entity.SysConfig;
import com.fau.wmbj.server.utils.SqlUtils;
import com.fau.wmbj.server.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 本地文件存储
 *
 * @author jiawei
 */
@Slf4j
@Component("localStorage")
public class LocalStorage extends Storage {
    @Resource
    private SqlUtils sqlUtils;

    @Override
    public String getName() {
        return "本地存储";
    }


    @Override
    public String upload(String path, File file, String contentType, boolean open) {
        SysConfig config = (SysConfig) sqlUtils.findOne("select * from f_config c where c.config_key = 'file_path'", SysConfig.class);
        File destFile = new File(config.getConfigValue() + path);
        try {
            FileUtils.copyFile(file, destFile);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return path;
    }

    /**
     * 文件上传
     *
     * @param path          上传路径
     * @param multipartFile 上传文件
     * @return String 文件路径
     */
    @Override
    public String upload(String path, MultipartFile multipartFile, boolean open) {
        SysConfig config = (SysConfig) sqlUtils.findOne("select * from f_config c where c.config_key = 'file_path'", SysConfig.class);
        File destFile = new File(config.getConfigValue() + path);
        try {
            String parent = destFile.getParent();
            File file = new File(parent);
            if (!file.exists()) {
                file.mkdirs();
            }
            multipartFile.transferTo(destFile);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return path;
    }

    @Override
    public void delete(StorageDirectory storageDirectory, boolean isPreview) {
        String url = isPreview ? storageDirectory.getPreview() : storageDirectory.getUrl();
        if (StringUtils.isNotBlank(url)) {
            SysConfig config = (SysConfig) sqlUtils.findOne("select * from f_config c where c.config_key = 'file_path'", SysConfig.class);
            File file = new File(config.getConfigValue() + url);
            file.delete();
        }
    }

    /**
     * 文件删除
     *
     * @param url
     * @param groupId
     */
    @Override
    public void delete(String url, String groupId) {
        if (StringUtils.isNotBlank(url)) {
            SysConfig config = (SysConfig) sqlUtils.findOne("select * from f_config c where c.config_key = 'file_path'", SysConfig.class);
            File file = new File(config.getConfigValue() + url);
            file.delete();
        }
    }


    /**
     * 获取访问URL
     *
     * @param storageDirectory 已上传文件
     * @return 访问URL
     */
    @Override
    public String getUrl(StorageDirectory storageDirectory, boolean isPreview, int timeOut) {
        String url = isPreview ? storageDirectory.getPreview() : "";
        return storageDirectory.getStorageServer() + url;
    }

    /**
     * 获取服务端存储路径
     *
     * @param diskPath 上传路径
     * @return 访问URL
     */
    @Override
    public String getStoragePath(String diskPath) {
        return diskPath;
    }

    /**
     * 下载文件
     *
     * @param storageDirectory 文件详情
     * @author jiawei
     */
    @Override
    public InputStream download(StorageDirectory storageDirectory, boolean isPreview) {
        String filePath = isPreview ? storageDirectory.getPreview() : storageDirectory.getUrl();
        SysConfig config = (SysConfig) sqlUtils.findOne("select * from f_config c where c.config_key = 'file_path'", SysConfig.class);

        File file = new File(config.getConfigValue() + filePath);
        if (file.exists()) {
            try {
                return new FileInputStream(file);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }
}